<template>
	<div>
		<div style="margin-bottom:20px">
			<el-alert title="如果枚举里有默认值项，请将它删除" type="success" :closable="false"></el-alert>
		</div>
		  <el-form :model="selectedParamInfo">
		    <el-form-item  v-for="(item,index) in inputValueList" key="jjj">
		      参数名 <el-input v-model="item.label" style="width:200px;display:inline-block"></el-input> 
		      <el-button @click.prevent="removeInputValue(item)" type="danger">删除</el-button>
		    </el-form-item>
			
		    <el-button type="info" @click="addInputValue">添加枚举</el-button>


		  </el-form>
		  <div slot="footer" class="dialog-footer">
		    <el-button @click="closeDialog">取 消</el-button>
		    <el-button type="primary" @click="dialogOkBtn">确 定</el-button>
		  </div>
	</div>
</template>

<script>
import _http from '../common/http.vue'

export default{
	props:['editDataInit'],
	data(){
		return{
			selectedParamInfo:{},
			inputValueList:[],
			startIndex:0
		}
	},
	mounted(){
	    this.inputValueList = this.dealJson(this.editDataInit.value)
	    this.startIndex = this.inputValueList[this.inputValueList.length-1]['value']+1
	},
	methods:{
		closeDialog(){
			this.$emit('closeDialog')
		},
		dealJson(item){//处理json数据
			return JSON.parse(item)
		},
		removeInputValue(item){//删除枚举值
    		let index = this.inputValueList.indexOf(item)
    		if (index !== -1) {
	          this.inputValueList.splice(index, 1)
	        }
    	},
    	addInputValue(){//动态添加枚举值
    		this.inputValueList.push({
    			label:'',
    		})
    	},
		dialogOkBtn(){
			for(let i in this.inputValueList){
				if (!this.inputValueList[i].hasOwnProperty('value')) {
					this.inputValueList[i]['value'] = this.startIndex
					this.startIndex++
				}
				
			}
			console.log(this.inputValueList)
			_http.axiosRequest(this,'configEditInputValueForm',{id:this.editDataInit.id,valueData:this.inputValueList},(res)=>{
    			let result = _http.apiResult(this,res.data)
    			if (result) {
    				this.closeDialog()
    				this.$emit('freshList')
    			}
    		})
		}
	}

}	
</script>